<?php

class Application_Model_Pagamento extends Zend_Db_Table_Abstract
{
		// Table Name
		protected $_name = 'pagamento';
		// Primary Key
		protected $_primary = 'id_pagamento';
	
		// Insert
		public function insert(array $data)
		{
			return	parent::insert($data);
		}
	
		// Update
		public function update(array $data, $where)
		{
			return parent::update($data, $where);
		}
	
		// Delete
		public function delete($where)
		{
			return parent::delete($where);
		}
	
		public function selectAll($future = false)
		{
			$select = $this->select()->setIntegrityCheck(false);
			
			$select->from(array('c' => $this->_name), array('c.valor','c.id_pagamento'));
			
			$select->join(array('d' => 'dia'), 'd.id_dia = c.id_dia',array(new Zend_Db_Expr("to_char(d.dia, 'dd-mm-YYYY') as dia")));
			
			$select->order('d.id_dia ASC');
			
			return $this->fetchAll($select);
		}
	
		public function selectCurrentValue()
		{
			$select = $this->select()->setIntegrityCheck(false);
			
			$select->from(array('p' => 'pagamento',array('p.id_dia','p.valor')));
			
			$select->order('p.id_dia DESC');
			
			$select->limit('6');
			
			$resultSet = $this->fetchAll($select);
			$saida = 0;
			foreach($resultSet as $i => $v)
			{
				$saida += $v['valor'];
			}
			
			return $saida/3;
			
		}
		public function selectRow($id)
		{
			$select = $this->select();
			$select->where('id_pagamento = ?',$id);
			return $this->fetchRow($select);
		}
}

